package com.CGFrame.struts.bean.material;

import java.util.Iterator;
import java.util.Vector;

import com.CGFrame.struts.POJOs.Materialgroup;
import com.CGFrame.struts.bean.ProjectOperate;
import com.CGFrame.struts.bean.ProjectOperateFactory;
import com.CGFrame.struts.bean.tree.TreeNode;

public class MaterialTree {
	
	
	private static TreeNode buildTree(Materialgroup mg)
	{
		TreeNode root = new  TreeNode();
		root.setNodeId(mg.getMaterialGroupId().toString());
		root.setNodeType("Materialgroup");
		root.setNodeName(mg.getMaterialGroupName());
		root.setNodeLink("Type=Materialgroup&ObjectID="+mg.getMaterialGroupId());
		Vector vec = new Vector();
		for(Iterator ite0 = mg.getMaterialgroups().iterator();ite0.hasNext();)
		{
			Materialgroup subMg = (Materialgroup)ite0.next();
			TreeNode subTreeNode = buildTree(subMg);
			vec.add(subTreeNode);
		}
		root.setChilds(vec);
		return root;
	}
	
	public static TreeNode buildTree()
	{
		try {
			
			ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
			Materialgroup mg = (Materialgroup) pop.Query(
					"from Materialgroup  as o where materialgroup is null ")
					.get(0);
			
			return buildTree(mg);
			
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	public static void main(String [] args)
	{
		ProjectOperate pop = ProjectOperateFactory.getprojectOperate();
		Materialgroup mg = (Materialgroup)pop.Query("from Materialgroup  as o where materialgroup is null ").get(0);
		TreeNode  root = buildTree(mg);
		
	}
	
}
